/*
 * Copyright 2007 Rob Nielsen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.proxymatic.core.builder;

import com.googlecode.proxymatic.core.BuildtimeContext;
import com.googlecode.proxymatic.core.RuntimeContext;
import com.googlecode.proxymatic.core.util.ReflectUtil;

public class ProxyObjectBuilder {
    private Object[] implementationObjects = {};
    private Class[] implementationClasses = {};
    private Class[] targetInterfaces;

    public ProxyObjectBuilder implementingInterface(Class targetInterface) {
        targetInterfaces = new Class[]{targetInterface};
        return this;
    }

    public ProxyObjectBuilder withImplementationObjects(Object... implementationObjects) {
        this.implementationObjects = implementationObjects;
        return this;
    }

    public RuntimeContext buildRuntimeContext() {
        if (targetInterfaces == null || targetInterfaces.length == 0) {
            throw new IllegalArgumentException("At least one target interface must be specified");
        }
        if (implementationObjects.length > 0 && implementationClasses.length == 0) {
            implementationClasses = ReflectUtil.createClassArray(implementationObjects);
        }
        BuildtimeContext buildtimeContext = new BuildtimeContext(targetInterfaces,
                implementationClasses, implementationObjects.length > 0);
        return new RuntimeContext(buildtimeContext, implementationObjects);
    }

    public ProxyObjectBuilder withImplementationObject(Object implementationObject) {
        this.implementationObjects = new Object[]{implementationObject};
        return this;
    }

    public ProxyObjectBuilder withImplementationClass(Class implementationClass) {
        this.implementationClasses = new Class[]{implementationClass};
        return this;
    }

    public ProxyObjectBuilder implementingInterfaces(Class[] targetInterfaces) {
        this.targetInterfaces = targetInterfaces;
        return this;
    }

    public ProxyObjectBuilder withImplementationClasses(Class[] implementationClasses) {
        this.implementationClasses = implementationClasses;
        return this;
    }
}
